#include <vector>
#include <iostream>

using namespace std;

class Solution 
{
public:
    void rotate(vector<vector<int>>& matrix) 
    {
        int n = matrix[0].size();
        // 先转置，再对称反转
        for(int i = 0; i < n; i++)
        {
            for(int j = i + 1; j < n; j++)
            {
                swap(matrix[i][j], matrix[j][i]);
            }
        }
        for(int j = 0; j < n / 2; j++)
        {
            for(int i = 0; i < n; i++)
            {
                swap(matrix[i][j], matrix[i][n - 1 - j]);
            }
        }
    }
};

// 辅助函数：打印矩阵
void printMatrix(const vector<vector<int>>& matrix) 
{
    cout << "[";
    for(int i = 0; i < matrix.size(); i++) 
    {
        cout << "[";
        for(int j = 0; j < matrix[i].size(); j++) 
        {
            cout << matrix[i][j];
            if(j < matrix[i].size() - 1) cout << ",";
        }
        cout << "]";
        if(i < matrix.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

int main() 
{
    Solution solution;
    
    // 测试案例1: [[1,2,3],[4,5,6],[7,8,9]]
    cout << "测试案例1:" << endl;
    vector<vector<int>> matrix1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    cout << "输入: matrix = ";
    printMatrix(matrix1);
    solution.rotate(matrix1);
    cout << "输出: ";
    printMatrix(matrix1);
    cout << "期望: [[7,4,1],[8,5,2],[9,6,3]]" << endl << endl;
    
    // 测试案例2: [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
    cout << "测试案例2:" << endl;
    vector<vector<int>> matrix2 = {{5, 1, 9, 11}, {2, 4, 8, 10}, {13, 3, 6, 7}, {15, 14, 12, 16}};
    cout << "输入: matrix = ";
    printMatrix(matrix2);
    solution.rotate(matrix2);
    cout << "输出: ";
    printMatrix(matrix2);
    cout << "期望: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]" << endl << endl;
    
    // 测试案例3: 1x1矩阵
    cout << "测试案例3 (1x1矩阵):" << endl;
    vector<vector<int>> matrix3 = {{1}};
    cout << "输入: matrix = ";
    printMatrix(matrix3);
    solution.rotate(matrix3);
    cout << "输出: ";
    printMatrix(matrix3);
    cout << "期望: [[1]]" << endl << endl;
    
    // 测试案例4: 2x2矩阵
    cout << "测试案例4 (2x2矩阵):" << endl;
    vector<vector<int>> matrix4 = {{1, 2}, {3, 4}};
    cout << "输入: matrix = ";
    printMatrix(matrix4);
    solution.rotate(matrix4);
    cout << "输出: ";
    printMatrix(matrix4);
    cout << "期望: [[3,1],[4,2]]" << endl << endl;
    
    // 测试案例5: 5x5矩阵
    cout << "测试案例5 (5x5矩阵):" << endl;
    vector<vector<int>> matrix5 = {
        {1, 2, 3, 4, 5},
        {6, 7, 8, 9, 10},
        {11, 12, 13, 14, 15},
        {16, 17, 18, 19, 20},
        {21, 22, 23, 24, 25}
    };
    cout << "输入: matrix = ";
    printMatrix(matrix5);
    solution.rotate(matrix5);
    cout << "输出: ";
    printMatrix(matrix5);
    cout << "期望: [[21,16,11,6,1],[22,17,12,7,2],[23,18,13,8,3],[24,19,14,9,4],[25,20,15,10,5]]" << endl << endl;
    
    // 测试案例6: 包含负数
    cout << "测试案例6 (包含负数):" << endl;
    vector<vector<int>> matrix6 = {{-1, -2, -3}, {-4, -5, -6}, {-7, -8, -9}};
    cout << "输入: matrix = ";
    printMatrix(matrix6);
    solution.rotate(matrix6);
    cout << "输出: ";
    printMatrix(matrix6);
    cout << "期望: [[-7,-4,-1],[-8,-5,-2],[-9,-6,-3]]" << endl << endl;
    
    // 测试案例7: 包含0
    cout << "测试案例7 (包含0):" << endl;
    vector<vector<int>> matrix7 = {{0, 1, 2}, {3, 0, 5}, {6, 7, 0}};
    cout << "输入: matrix = ";
    printMatrix(matrix7);
    solution.rotate(matrix7);
    cout << "输出: ";
    printMatrix(matrix7);
    cout << "期望: [[6,3,0],[7,0,1],[0,5,2]]" << endl << endl;
    
    // 测试案例8: 大数值
    cout << "测试案例8 (大数值):" << endl;
    vector<vector<int>> matrix8 = {{100, 200}, {300, 400}};
    cout << "输入: matrix = ";
    printMatrix(matrix8);
    solution.rotate(matrix8);
    cout << "输出: ";
    printMatrix(matrix8);
    cout << "期望: [[300,100],[400,200]]" << endl << endl;
    
    // 测试案例9: 6x6矩阵
    cout << "测试案例9 (6x6矩阵):" << endl;
    vector<vector<int>> matrix9 = {
        {1, 2, 3, 4, 5, 6},
        {7, 8, 9, 10, 11, 12},
        {13, 14, 15, 16, 17, 18},
        {19, 20, 21, 22, 23, 24},
        {25, 26, 27, 28, 29, 30},
        {31, 32, 33, 34, 35, 36}
    };
    cout << "输入: matrix = ";
    printMatrix(matrix9);
    solution.rotate(matrix9);
    cout << "输出: ";
    printMatrix(matrix9);
    cout << "期望: [[31,25,19,13,7,1],[32,26,20,14,8,2],[33,27,21,15,9,3],[34,28,22,16,10,4],[35,29,23,17,11,5],[36,30,24,18,12,6]]" << endl << endl;
    
    // 测试案例10: 相同元素
    cout << "测试案例10 (相同元素):" << endl;
    vector<vector<int>> matrix10 = {{5, 5, 5}, {5, 5, 5}, {5, 5, 5}};
    cout << "输入: matrix = ";
    printMatrix(matrix10);
    solution.rotate(matrix10);
    cout << "输出: ";
    printMatrix(matrix10);
    cout << "期望: [[5,5,5],[5,5,5],[5,5,5]]" << endl << endl;
    
    return 0;
}